package servlet;

import model.Department;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@WebServlet("/deleteSelected.action")
public class DeleteSomePart extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        File file = new File("C:\\Users\\FeiFei\\Desktop\\test.txt");
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        List<Department> partList = null;
        try {
            partList = (List<Department>) ois.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String[] checkedIDS = request.getParameterValues("checkedID");
        for (String checkedID : checkedIDS) {
            int checked=Integer.parseInt(checkedID);
            partList.remove(checked);
        }
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write("");
        fileWriter.flush();
        fileWriter.close();

        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(partList);
        oos.close();
        fos.close();
        response.sendRedirect(request.getContextPath() + "/partInfo.jsp");
        // 在这里编写删除选中数据的逻辑
        // 根据选中的数据进行删除操作

        // 删除成功后，可以返回适当的响应给前端

    }
}
